package com.wallet.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.wallet.entity.Article;
import com.wallet.entity.example.ArticleExample;
import com.wallet.entity.form.ArticleForm;
import com.wallet.entity.form.ArticlesForm;
import com.wallet.entity.result.JsonResult;
import com.wallet.entity.result.ResultCode;
import com.wallet.service.BaseService;
import com.wallet.util.HttpClientUtil;

@RestController
@RequestMapping("/article")
public class ArticleController {
	
	@Autowired	
	private BaseService<Article, ArticleExample> articleService; 
	
	@ResponseBody
	@RequestMapping(value = "/list")
	public JsonResult list(Integer page, Integer limit){
		return articleService.showPage(page, limit);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getDetail")
	public Article getDetail(Integer id) {
		return articleService.getDetail(id);
	}
	
	@ResponseBody
	@RequestMapping(value = "/create")
	public JsonResult create(@RequestBody Article article) {
		article.setId(null);
		articleService.save(article);
		JsonResult result=new JsonResult();
		result.setCode(ResultCode.SUCCESS.getVal());
		result.setData("success");
		return result;
	}

	private Article ToArticle(ArticleForm articleForm) {
		Article article=new Article();
		article.setTimestamp(articleForm.getTimestamp());
		article.setAuthor(articleForm.getAuthor());
		article.setReviewer(articleForm.getReviewer());
		article.setTitle(articleForm.getTitle());
		article.setContent_short(articleForm.getContent_short());
		article.setContent(articleForm.getContent());
		article.setForecast(articleForm.getForecast());
		article.setImportance(articleForm.getImportance());
		article.setType(articleForm.getType());
		article.setStatus(articleForm.getStatus());
		article.setDisplay_time(articleForm.getDisplay_time());
		article.setComment_disabled(articleForm.getComment_disabled());
		article.setPageviews(articleForm.getPageviews());
		article.setImage_uri(articleForm.getImage_uri());
		article.setPlatforms(articleForm.getPlatforms());
		if(articleForm.getId()!=null) {
			article.setId(articleForm.getId());
		}
		return article;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update")
	public JsonResult update(@RequestBody ArticleForm article) {
		Article articledp=ToArticle(article);
		articleService.upd(articledp);
		JsonResult result=new JsonResult();
		result.setCode(ResultCode.SUCCESS.getVal());
		result.setData("success");
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/init")
	public int initarticle(){
		String result=HttpClientUtil.get("http://localhost:9527/dev-api/vue-element-admin/article/list?page=1&limit=100");
		Map<String,Object> maps = (Map<String,Object>)JSON.parse(result);  
		Map<String,Object> data=(Map<String,Object>)maps.get("data");
		String items=data.get("items").toString();
		List<ArticlesForm> articleStart=JSON.parseArray(items, ArticlesForm.class);
		List<Article> articleEnd=new ArrayList<Article>();
		for (ArticlesForm articleForm : articleStart) {
			Article article=new Article();
			article.setId(articleForm.getId());
			article.setTimestamp(articleForm.getTimestamp());
			article.setAuthor(articleForm.getAuthor());
			article.setReviewer(articleForm.getReviewer());
			article.setTitle(articleForm.getTitle());
			article.setContent_short(articleForm.getContent_short());
			article.setContent(articleForm.getContent());
			article.setForecast(articleForm.getForecast());
			article.setImportance(articleForm.getImportance());
			article.setType(articleForm.getType());
			article.setStatus(articleForm.getStatus());
			article.setDisplay_time(articleForm.getDisplay_time());
			article.setComment_disabled(articleForm.getComment_disabled());
			article.setPageviews(articleForm.getPageviews());
			article.setImage_uri(articleForm.getImage_uri());
			String pla="";
			if(articleForm.getPlatforms().length>0) {
				for (String plat : articleForm.getPlatforms()) {
					if(pla.length()==0) {
						pla+=plat;	
					}else {
						pla=pla+","+plat;
					}
					
				}
				article.setPlatforms(pla);
			}
			articleEnd.add(article);
		}
		return articleService.insertBatch(articleEnd);
	}
}
